import Vue from "vue";
import BaseDrawer from "./BaseDrawer.vue";

function getDrawerConstructor(ctxCpm, draProps) {
    return Vue.extend({
        render(h, hack) {
            return h(
                BaseDrawer, {
                    props: {
                        ...draProps,
                    }
                },
                [h(ctxCpm, {
                    slot: 'content',
                    props: draProps.formProps
                })]
            )
        }
    });
}

export const useDrawer = (ctxCpm, draProps) => {
    let constructor = getDrawerConstructor(ctxCpm, draProps);
    const drawer = new constructor();
    const div = document.createElement('div');
    document.body.appendChild(div);
    const drawInstance = drawer.$mount(div);
    return () => {
        drawInstance.$el.remove();
        drawInstance.$destroy();
    }
}
